package com.kuzlak.cms.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuzlak.cms.bean.MyPage;
import com.kuzlak.cms.module.sys.entity.SysDictValue;
import com.kuzlak.cms.module.sys.mapper.SysDictValueMapper;
import com.kuzlak.cms.module.sys.service.SysDictValueService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author hesenjan
* @description 针对表【sys_dict_value】的数据库操作Service实现
* @createDate 2024-11-30 23:46:05
*/
@Service
public class SysDictValueServiceImpl extends ServiceImpl<SysDictValueMapper, SysDictValue>
    implements SysDictValueService{
    @Cacheable(cacheNames = "findByKey", key = "#key")
    @Override
    public List<SysDictValue> findByKey(String key) {
        QueryWrapper<SysDictValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql("DICT_ID", "SELECT ID FROM sys_dict where IS_KEY = '" + key + "'");
        List<SysDictValue> byKey = this.baseMapper.selectList(queryWrapper);
        return byKey;
    }

    @Override
    public boolean deleteById(long id) {

        int i = this.baseMapper.deleteById(id);
        return (i != 0);
    }

    @Override
    public MyPage findByPage(int pagesize, int page, String name, int status, long dictValueId, String remark, String isValue) {
        QueryWrapper<SysDictValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysDictValue::getDeleted, 0);

        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.lambda().likeRight(SysDictValue::getName, name);
        }

        if (StringUtils.isNotEmpty(remark)) {
            queryWrapper.lambda().likeRight(SysDictValue::getRemark, remark);
        }
        if (StringUtils.isNotEmpty(isValue)) {
            queryWrapper.lambda().likeRight(SysDictValue::getIsValue, isValue);
        }


        if (dictValueId > 0) {
            queryWrapper.lambda().eq(SysDictValue::getDictId, dictValueId);
        }
        if (status > 0) {
            queryWrapper.lambda().eq(SysDictValue::getStatus, status);
        }
        Page<SysDictValue> page0 = new Page<>(page, pagesize);
        IPage<SysDictValue> data = this.baseMapper.selectPage(page0, queryWrapper);


        return MyPage.parsePage(data);
    }

    @Cacheable(cacheNames = "findByKeys", key = "#keys")
    @Override
    public Map<String, List<SysDictValue>> findByKeys(String keys) {
        Map<String, List<SysDictValue>> result = new HashMap<>();
        String[] split = keys.split(",");
        for (String s : split) {
            if (StringUtils.isNotEmpty(s)) {
                System.out.println(s);
                List<SysDictValue> byKey = this.findByKeyMap(s.trim());
                result.put(s.trim(), byKey);
            }

        }
        return result;
    }

    private List<SysDictValue> findByKeyMap(String trim) {
        System.out.println(trim);
        QueryWrapper<SysDictValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql("DICT_ID", "SELECT ID FROM sys_dict where IS_KEY = '" + trim + "'");
        return this.baseMapper.selectList(queryWrapper);
    }
}





